// fenxiao/tixian/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.GetDistributorInfo()
  },
  //佣金提現 
  formSubmit:function(e){
    console.log(e.detail.value);
    var money = e.detail.value.money;
    var phone = e.detail.value.phone;
    if (app.globalData.userInfo.Uid ==''){
       return
    }
    if (money == '') {
      wx.showModal({
        title: '提示',
        content: "请输入金额",
        showCancel: false
      })
      return
    }
    if (phone == '') {
      wx.showModal({
        title: '提示',
        content: "请输入电话",
        showCancel: false
      })
      return
    }
    else{
      wx.showModal({
        title: '提示',
        content: '是否确定提现！',
        success: function (res) {
          if (res.confirm) {
            wx.request({
              url: app.globalData.host + '/api/Distributor/BrokerageRequest',
              data: {
                userId: app.globalData.userInfo.Uid,
                storeId: app.globalData.storeId,
                money: money,
                phone: phone
              },
              success: function (res) {
                console.log(res);
                if (res.data.Code == 0) {
                  wx.showToast({
                    title: '提现成功',
                    icon: 'success',
                    duration: 2000,
                    success:function(){
                      wx.navigateTo({
                        url: '/fenxiao/tixianRecords/index',
                      })
                    }
                  })
                } else {
                  wx.showModal({
                    title: '提示',
                    content: res.data.Message,
                    showCancel: false
                  })
                  return
                }
              }
            })
          } else if (res.cancel) {
            return
          }
        }
      })
    }
  },
  //获取分销商信息，如果分销商不存在，则创建新的
  GetDistributorInfo: function () {
    var that = this;
    var disId = wx.getStorageSync('disid')
    var agentId = wx.getStorageSync('agentid')
    if (disId == "" || disId == null) {
      disId = 0
    }
    if (agentId == "" || agentId == null) {
      agentId = 0
    }
    wx.request({
      url: app.globalData.host + '/api/Distributor/GetDistributorAndCreate',
      data: {
        userId: app.globalData.userInfo.Uid,
        disId: disId,
        agentId: agentId,
        storeId: app.globalData.storeId
      },
      success: function (res) {
        console.log("获取分销商信息");
        console.log(res);
        if (res.data.Code == 0) {
          var list = res.data.Data
          console.log(list);
          that.setData({
            lists: list,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.GetDistributorInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})